package UserAndGroup;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;

import java.util.List;

/**
 * <p>类描述：查询用户所属的用户组。</p>
 *
 * @author 贾亮
 * @version v1.0.0.1。
 * @since JDK1.8。
 * <p>创建日期：2018/3/6 10:48。</p>
 */
public class GroupOfUser {
    public static void main(String[] args) {
        // 创建流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 得到身份服务组件实例
        IdentityService identityService = processEngine.getIdentityService();
        // 保存一个用户
        User user = Creat.creatUser(identityService, "3", "张三", "yong", "yangenxiong@163.com", "abc");
        // 保存两个用户组
        Group group1 = Creat.createGroup(identityService, "2", "经理组", "manager");
        Group group2 = Creat.createGroup(identityService, "3", "员工组", "manager");
        // 将用户分配到两个用户组下
        identityService.createMembership(user.getId(), group1.getId());
        identityService.createMembership(user.getId(), group2.getId());
        // 查询用户所属的用户组
        List<Group> list = identityService.createGroupQuery().groupMember(user.getId()).list();
        System.out.println("张三属于的用户组有：");
        for (Group group : list) {
            System.out.println(group.getName());
        }
        // 关闭流程引擎，删除表
        processEngine.close();
    }
}
